package controllers
import (
	//"github.com/astaxie/beego"
	m"LiquidMeasurement/models"

//."LiquidMeasurement/forms"
	//"fmt"
//	"github.com/astaxie/beego/validation"
	//m"LiquidMeasurement/models"
//	"github.com/astaxie/beego/context"
//	"net/url"
//	."LiquidMeasurement/lib"
	//"time"
	"strconv"
//	"github.com/astaxie/beego/cache"
	"LiquidMeasurement/forms"
	"fmt"
"LiquidMeasurement/lib"
)
type BedController struct {
	AdminController
}



/*********************************************************/
/************函数：CreateBed********************************/
/************功能：添加新的床位****************************/
/*********************************************************/
func (this *BedController) CreateBed() {
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		//	获取网页请求的方式
		admin := (this.GetSessionAdmin(UserID))
		if method := this.Ctx.Input.Method();method == "POST" {       //判断请求方式
			var form = new(forms.BedManageForm)
			Errors := make([]lib.ERROR,0,10)
			if err := this.ParseForm(form); err == nil {
				if _,err_msg :=form.CheckValid();len(err_msg) != 0{
					Errors = append(Errors,err_msg...)
				}else{
					department, err1 := m.GetDepartmnetById(form.Department)
					room, err2 := m.GetRoomById(form.Room)
					if ((err1 == nil)&&(err2 == nil)) {
						bed := m.Bed{BedNum:form.BedNum, Department_:&department, Room_:&room}
						_, err := m.AddBed(&bed)
						fmt.Println("err",err,"b",bed)
						if (err == nil) {
							fmt.Println("bed", bed.Room_.RoomNum)
							this.Ctx.Redirect(302, "/admin/show_beds_list/" + UserID)
							return
						}else {
							//fmt.Println("err", err)
							Errors = append(Errors,lib.ERROR{0,"数据库错误"})
						}

					}else {
						Errors = append(Errors,lib.ERROR{0,"数据库错误"})
					}
				}
			}else{
				Errors = append(Errors,lib.ERROR{0,"表单验证错误"})

			}
			departments, _ := m.GetDepartmentList()
			rooms, _ := m.GetRoomList()
			this.Data["departments"] = departments
			this.Data["rooms"] = rooms
			this.Data["UserID"] = UserID
			this.Data["Errors"] = &Errors
			this.Data["admin"] = &admin
			this.TplName = "add_bed.html"

		}else {
			departments, _ := m.GetDepartmentList()
			rooms, _ := m.GetRoomList()
			this.Data["departments"] = departments
			this.Data["rooms"] = rooms
			this.Data["UserID"] = UserID
			this.Data["admin"] = &admin

			this.TplName = "add_bed.html"
			return
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}

/*********************************************************/
/************函数：DeleteBed********************************/
/************功能：删除床位****************************/
/*********************************************************/
func (this *BedController) DeleteBed() {
	method := this.Ctx.Input.Method()
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {

		if method == "POST" {       //判断请求方式
			var form = new(forms.BedManageForm)
			//this.ParseForm(&mLoginType)
			if err := this.ParseForm(form); err != nil {
				respone_json := forms.SiteResponeType{forms.SITE_DELETE_ERR, forms.INVALIDS, forms.INVALIDS}
				this.Data["json"] = &respone_json
				this.ServeJSON();
			}
			//department := m.Department{Id:form.Id,Name:form.Name,Detail:form.Detail}
			m.DelBedByBedID(form.Id)
			fmt.Println("form:", form)
			respone_json := forms.SiteResponeType{forms.SITE_SUCCESS, forms.INVALIDS, forms.INVALIDS}
			this.Data["json"] = &respone_json
			this.ServeJSON();
			//this.TplName = "show_departments.html"
			return
		}else {
			respone_json := forms.SiteResponeType{forms.SITE_METHOD_ERROR, forms.INVALIDS, forms.INVALIDS}
			this.Data["json"] = &respone_json
			this.ServeJSON();
			return
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}

}

/*********************************************************/
/************函数：GetBedInfoById********************************/
/************功能：获取床位信息****************************/
/*********************************************************/
func (this *BedController) GetBedInfoById() {
//	if islogin,_ := this.CheckBedIsLogin();islogin == true {
//		BedID := this.Ctx.Input.Param(":BedID")    //从url中提取数据
//		data := this.GetSession(BedID)          //从session中提取数据
//		var mBedSessionType BedSessionType
//		if data == nil {                       //数据为空
//			var beds  interface{}
//			this.Data["json"] = &beds
//			this.ServeJSON();
//			return
//		}else {
//			mBedSessionType = data.(BedSessionType)    //数据格式转换
//			if mBedSessionType.IsLogin == false {
//				var beds  interface{}
//				this.Data["json"] = &beds
//				this.ServeJSON();
//				return
//			}else {
//				if ((mBedSessionType.LastDate.Unix()-time.Now().Unix()) > ONEDAY2SEC) {
//					var beds  interface{}
//					this.Data["json"] = &beds
//					this.ServeJSON();
//					return
//				}
//			}
//		}
//		beds, _ := m.GetBedList()
//		this.Data["json"] = &beds
//		this.ServeJSON();
//	}else{
//		this.Ctx.Redirect(302, "/admin/getuser")
//		return
//	}
}


func (this *BedController) GetBedInfoByName() {
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplName = "index.tpl"
}

/*********************************************************/
/************函数：UpdateBed********************************/
/************功能：更新床位信息****************************/
/*********************************************************/
func (this *BedController) UpdateBed() {
	method := this.Ctx.Input.Method()
	UserID := this.Ctx.Input.Param(":UserID")
	id := this.Ctx.Input.Param(":id")
	Id, _ := strconv.ParseUint(id, 10, 64)

	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		if method == "POST" {
			var form = new(forms.BedManageForm)
			Errors := make([]lib.ERROR,0,10)
			if err := this.ParseForm(form); err == nil {
				if _, err_msg := form.CheckValid(); len(err_msg) != 0 {
					Errors = append(Errors, err_msg...)
				}else {
					room, _ := m.GetRoomById(form.Room)
					department, _ := m.GetDepartmnetById(form.Department)
					bed := m.Bed{Id:form.Id, BedNum:form.BedNum, Room_:&room, Department_:&department}
					_, err := m.UpdateBed(&bed)

					if (err == nil) {
						//this.Data["department"] = &department
						this.Ctx.Redirect(302, "/admin/show_beds_list/" + UserID)

						return
					}else {
						Errors = append(Errors, lib.ERROR{0, "数据库更新错误"})
					}
				}
			}

			departments, _ := m.GetDepartmentList()
			model, _ := m.GetBedByBedID(Id)
			this.Data["bed"] = &model
			this.Data["admin"] = &admin
			this.Data["departments"] = &departments
			this.Data["Errors"] = &Errors
			this.TplName = "update_bed.html"
			return
		}else {

			departments, _ := m.GetDepartmentList()
			model, err := m.GetBedByBedID(Id)
			if (err == nil) {
				this.Data["bed"] = &model
				this.Data["departments"] = &departments
				this.Data["admin"] = &admin
				this.TplName = "update_bed.html"
				return
			}else {
				this.Abort("401")
				return
			}
		}
	}else{
		this.Ctx.Redirect(302, "/admin/login")
	}
}

/*********************************************************/
/************函数：GetBedInfo********************************/
/************功能：获取床位信息****************************/
/*********************************************************/
func (this *BedController) GetBedInfo(id uint64) {
//		BedID := this.Ctx.Input.Param(":BedID")    //从url中提取数据
//		data := this.GetSession(BedID)          //从session中提取数据
//		var mBedSessionType BedSessionType
//		if data == nil {                       //数据为空
//			var bed  interface{}
//			this.Data["json"] = &bed
//			this.ServeJSON();
//			return
//		}else {
//			mBedSessionType = data.(BedSessionType)    //数据格式转换
//			if mBedSessionType.IsLogin == false {
//				var bed  interface{}
//				this.Data["json"] = &bed
//				this.ServeJSON();
//				return
//			}else {
//				if ((mBedSessionType.LastDate.Unix()-time.Now().Unix()) > ONEDAY2SEC) {
//					var bed  interface{}
//					this.Data["json"] = &bed
//					this.ServeJSON();
//					return
//				}
//			}
//		}
//		bed, _ := m.GetBedList()
//		this.Data["json"] = &bed
//		this.ServeJSON();
}



func (this *BedController)HandleWarning()  {

}

func (this *BedController) ShowBedsList() {
	UserID := this.Ctx.Input.Param(":UserID")
	if islogin,_ := this.CheckAdminIsLogin(UserID);islogin == true {
		admin := (this.GetSessionAdmin(UserID))
		model, _ := m.GetBedList()
		//admin ,_ := m.GetAdminByUserIDS(UserID)
		this.Data["UserID"] = UserID
		fmt.Println("model", model)
		this.Data["beds"] = &model
		this.Data["admin"] = &admin
		this.TplName = "show_beds.html"
		return
	}else{
		this.Ctx.Redirect(302, "/admin/login")
		return
	}
}

func (this *BedController) GetBedsList() {

	model,_ :=m.GetBedList()

	UserID := this.Ctx.Input.Param(":UserID")
	this.Data["UserID"] = UserID
	fmt.Println("model",model)
	this.Data["beds"] = &model
	this.TplName="admin_index.html"
	return
}


func (this *BedController) SendWarning() {
	this.Data["Website"] = "beego.me"
	this.Data["Email"] = "astaxie@gmail.com"
	this.TplName = "index.tpl"
}


